//
//  CZNewFeatureViewController.swift
//  weibo10
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 apple. All rights reserved.
/*
1 延展CZNewFeatureViewController
2 numberOfItemsInSection cell的个数
3 cellForItemAtIndexPath  cell的样式
4 布局 UICollectionViewFlowLayout
5 实现init
6 在 func prepareCollectionView方法里设置layout参数


*/

import UIKit

private let reuseIdentifier = "Cell"

private let ItemsCount = 4

class CZNewFeatureViewController: UICollectionViewController {
    //MARK: - 属性
    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   init()
   {
    
    super.init(collectionViewLayout: layout)
    
    }
   
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
    }

    func prepareCollectionView()
    {
        
    self.collectionView!.registerClass(CZNewFeatureCell
        .self, forCellWithReuseIdentifier: reuseIdentifier)
        
      //设置layout参数  让cell和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //间距
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //取消弹簧效果
        collectionView?.bounces = false
        //不要水平滚动条
//        collectionView?.showsHorizontalScrollIndicator = false
        //分页
        collectionView?.pagingEnabled = true
        
    }
    
}

//延展要放最下面
extension CZNewFeatureViewController
{
//每一组显示几个cell
    override func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int)
    -> Int
    {
    return ItemsCount
    
    }
    
    //每个cell长什么样
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        cell.index = indexPath.item
        cell.backgroundColor = UIColor.brownColor()
        
        return cell
    }
    
    //当scrollView停止滚动  调用Button 的startAnimation方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView)
    {
        //获取collectionView的偏移值，计算当前页面
        let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
        
        //判断是否是最后一页
        if index == ItemsCount - 1
        {
        //获取cell 不要自己创建cell  要拿到正在显示的cell
            
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! CZNewFeatureCell
            cell.startAnimation()
        
        
        }
        
        
    }
    
    
    
    

}


